package references2bibtexrcp.core;

import java.io.File;
import java.io.FilenameFilter;

import org.eclipse.core.filesystem.EFS;
import org.eclipse.core.filesystem.IFileStore;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.ide.FileStoreEditorInput;
import org.eclipse.ui.part.ViewPart;

/**
 * 
 * @author v
 *
 */
public class View extends ViewPart {
	public static final String ID = "References2BibTexRCP.references";
	private Action doubleClickAction;

	private TableViewer viewer;

	/**
	 * The content provider class is responsible for providing objects to the
	 * view. It can wrap existing objects in adapters or simply return objects
	 * as-is. These objects may be sensitive to the current input of the view,
	 * or ignore it and always show the same content (like Task List, for
	 * example).
	 */
	class ViewContentProvider implements IStructuredContentProvider {
		public void inputChanged(Viewer v, Object oldInput, Object newInput) {
		}

		public void dispose() {
		}

		public Object[] getElements(Object parent) {
			if (parent instanceof Object[]) {
				return (Object[]) parent;
			}
	        return new Object[0];
		}
	}

	class ViewLabelProvider extends LabelProvider implements
			ITableLabelProvider {
		public String getColumnText(Object obj, int index) {
			return getText(obj);
		}

		public Image getColumnImage(Object obj, int index) {
			return getImage(obj);
		}

		public Image getImage(Object obj) {
			return Activator.getDefault().getImage(Activator.IMG_FORM_PXT);
		}
	}

	/**
	 * This is a callback that will allow us to create the viewer and initialize
	 * it.
	 */
	public void createPartControl(Composite parent) {
		viewer = new TableViewer(parent, SWT.MULTI | SWT.H_SCROLL
				| SWT.V_SCROLL);
		viewer.setContentProvider(new ViewContentProvider());
		viewer.setLabelProvider(new ViewLabelProvider());
		// Provide the input to the ContentProvider
		FilenameFilter filter = new FilenameFilter() {
		    public boolean accept(File dir, String name) {
		        return name.endsWith(".pxt");
		    }
		};
		
		
		System.out.println(new File(new File("").getAbsolutePath()).listFiles(filter).length);
		viewer.setInput(new File(new File("").getAbsolutePath()).listFiles(filter));
		
		hookDoubleClickAction();
		contributeActions();
		getSite().setSelectionProvider(viewer);
	}
	
	private void hookDoubleClickAction() {
		viewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				doubleClickAction.run();
			}
		});

	}

	private void contributeActions() {
		doubleClickAction = new Action() {
			public void run() { 
				if (true/*Login.State*/) {
					ISelection selection = viewer.getSelection();
					Object obj = ((IStructuredSelection) selection).getFirstElement();
					//EditorsNaming input = new EditorsNaming(obj.toString());
					
					IWorkbenchPage page = null;
					try {
						page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
					} catch (Exception e1) {
						e1.printStackTrace();
					}
					
					try {
						
						File file = new File(obj.toString());
						IFileStore fileStore= EFS.getStore(file.toURI());
						//page.openEditor(new FileStoreEditorInput(fileStore), ReferenceEditor.ID);
						page.openEditor(new FileStoreEditorInput(fileStore),  EditorsUI.DEFAULT_TEXT_EDITOR_ID);
					} catch (PartInitException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (CoreException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				} 
			}
		};
	}

	/**
	 * Passing the focus request to the viewer's control.
	 */
	public void setFocus() {
		viewer.getControl().setFocus();
	}
	
	public void update() {
		FilenameFilter filter = new FilenameFilter() {
		    public boolean accept(File dir, String name) {
		        return name.endsWith(".pxt");
		    }
		};
		
		
		System.out.println(new File(new File("").getAbsolutePath()).listFiles(filter).length);
		viewer.setInput(new File(new File("").getAbsolutePath()).listFiles(filter));
	}
}